package cn.darkhorse.mesadmin.task;

import cn.darkhorse.common.config.DbTypeConfig;
import cn.darkhorse.common.util.HttpClientUtils;
import cn.darkhorse.device.iot.constant.IotConstant;
import cn.darkhorse.device.pojo.MesDevice;
import cn.darkhorse.device.service.MesDeviceService;
import cn.darkhorse.pick.pojo.CottonApiModel;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import com.ruoyi.common.config.ApiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 第三方系统数据处理定时任务
 * @author yuanjun
 * @since 20231108
 */

@Component("GetPickerTask")
public class GetPickerTask {

    @Resource
    private ApiConfig apiConfig;

    @Resource
    private MesDeviceService mesDeviceService;


    public void doParse() {
        String URL = "https://zs.ecottone.com/api/getPickerList";

        JSONObject object = new JSONObject();

        object.put("deptId",apiConfig.getDeptId());

        String s = sendPost(URL, object);

        JSONObject parse = JSONObject.parse(s);


        mesDeviceService.lambdaUpdate().remove();
        parse.getJSONArray("data").forEach(o->{
            JSONObject jsonObject = (JSONObject) o;
            MesDevice device = new MesDevice();
            device.setCarBrand(jsonObject.getString("carBrand"));
            device.setCarNumber(jsonObject.getString("carNumber"));
            device.setId(jsonObject.getLong("pickerId"));
            device.setName(jsonObject.getString("name"));
            device.setCode(jsonObject.getString("pickerCode"));
            device.setHostName(jsonObject.getString("hostName"));
            device.setHostTel(jsonObject.getString("hostTel"));
            device.setDriver(jsonObject.getString("driver"));
            device.setTelephone(jsonObject.getString("telephone"));
            device.setLongitude(jsonObject.getString("longitude"));
            device.setLatitude(jsonObject.getString("latitude"));
            device.setSpeed(jsonObject.getString("speed"));
            device.setDirection(jsonObject.getString("direction"));
            device.setArea(jsonObject.getString("area"));
            device.setPickerStatus(jsonObject.getString("pickerStatus"));
            device.setGpsTime(jsonObject.getString("gpsTime"));
            device.setStatus(jsonObject.getInteger("status"));
            device.setPickerStatus(jsonObject.getString("pickerStatus"));
            device.setDept(Long.parseLong(jsonObject.getString("deptId")));
            device.setRemark(jsonObject.getString("remark"));
            mesDeviceService.save( device);
        });


    }


    @NotNull
    private String sendPost(String URL, JSONObject object) {
        String timedStamp = timeStamp();
        String sign = Md5Utils.hash(apiConfig.getServiceName() + apiConfig.getKey() + timedStamp);
        CottonApiModel cottonApiModel = new CottonApiModel(apiConfig.getServiceName(), timedStamp, sign, com.alibaba.fastjson2.JSONObject.toJSONString(object));
        String jsonString = JSONObject.toJSONString(cottonApiModel);
        System.out.println(jsonString);
        String s = null;
        try {
            s = HttpClientUtils.doPostJson(URL, JSONObject.toJSONString(cottonApiModel));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return s;
    }


    /**
     * 取得当前时间戳（精确到秒）
     * @return
     */
    public static String timeStamp(){
        long time = System.currentTimeMillis();
        String t = String.valueOf(time/1000);
        return t;
    }
}
